home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / thomas / thomas.lha / Thomas / Thomas-1.1 / dylan-examples.dyl < prev    next >
Text File  |  1992-09-22  |  32KB  |  1,861 lines

  1. ;;;; -*- Scheme -*- isn't Thomas (or Dylan(TM))
  2. ;*              Copyright 1992 Digital Equipment Corporation
  3. ;*                         All Rights Reserved
  4. ;*
  5. ;* Permission to use, copy, and modify this software and its documentation is
  6. ;* hereby granted only under the following terms and conditions.  Both the
  7. ;* above copyright notice and this permission notice must appear in all copies
  8. ;* of the software, derivative works or modified versions, and any portions
  9. ;* thereof, and both notices must appear in supporting documentation.
  10. ;*
  11. ;* Users of this software agree to the terms and conditions set forth herein,
  12. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  13. ;* right and license under any changes, enhancements or extensions made to the
  14. ;* core functions of the software, including but not limited to those affording
  15. ;* compatibility with other hardware or software environments, but excluding
  16. ;* applications which incorporate this software.  Users further agree to use
  17. ;* their best efforts to return to Digital any such changes, enhancements or
  18. ;* extensions that they make and inform Digital of noteworthy uses of this
  19. ;* software.  Correspondence should be provided to Digital at:
  20. ;*
  21. ;*                      Director, Cambridge Research Lab
  22. ;*                      Digital Equipment Corp
  23. ;*                      One Kendall Square, Bldg 700
  24. ;*                      Cambridge MA 02139
  25. ;*
  26. ;* This software may be distributed (but not offered for sale or transferred
  27. ;* for compensation) to third parties, provided such third parties agree to
  28. ;* abide by the terms and conditions of this notice.
  29. ;*
  30. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  31. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  32. ;* MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  33. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  34. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  35. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  36. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  37. ;* SOFTWARE.
  38.  
  39. ; $Id: dylan-examples.dyl,v 1.16 1992/09/23 04:43:53 birkholz Exp $
  40.  
  41. ;;; This is a copy of examples-from-book.text modified to be runnable in
  42. ;;; the Thomas REP.  The expected return value is given after ";Value: ".
  43. ;;; The book doesn't always show return values, so this value is what you
  44. ;;; can expect from Thomas.  Printed output appears after ";" before
  45. ;;; ";Value: ".  Error messages are quoted from the book.  Thomas doesn't
  46. ;;; report errors in exactly the same way.  Definitions added to make the
  47. ;;; example work are flagged by ";;; +".  Notes about differences from the
  48. ;;; examples as given in the book are prefixed by ";;; ".
  49.  
  50.  
  51. ;; Page 27
  52.  
  53. "abc"
  54. ;Value: "abc"
  55.  
  56. 123
  57. ;Value: 123
  58.  
  59. foo:
  60. ;Value: foo:
  61.  
  62. #\a
  63. ;Value: #\a
  64.  
  65. #t
  66. ;Value: #t
  67.  
  68. #f
  69. ;Value: #f
  70.  
  71. (quote foo)
  72. ;Value: foo
  73.  
  74. 'foo
  75. ;Value: foo
  76.  
  77. '(1 2 3)
  78. ;Value: (1 2 3)
  79.  
  80.  
  81. ;; Page 28
  82.  
  83. ;;; +
  84. (define-class <window> (<object>))
  85. ;Value: <window>
  86.  
  87. <window>
  88. ;Value: #[dylan-class ...]
  89.  
  90. concatenate
  91. ;Value: #[generic function ...]
  92.  
  93. (define my-variable 25)
  94. ;Value: my-variable
  95.  
  96. my-variable
  97. ;Value: 25
  98.  
  99. (bind ((x 50))
  100.   (+ x x))
  101. ;Value: 100
  102.  
  103. (setter element)
  104. ;Value: #[generic function ...]
  105.  
  106. (define (setter my-variable) 20)
  107. ;Value: (setter my-variable)
  108.  
  109. (setter my-variable)
  110. ;Value: 20
  111.  
  112.  
  113. ;; Page 29
  114.  
  115. (+ 3 4)
  116. ;Value: 7
  117.  
  118. (* my-variable 3)
  119. ;Value: 75
  120.  
  121. (* (+ 3 4) 5)
  122. ;Value: 35
  123.  
  124. ((if #t + *) 4 5)
  125. ;Value: 9
  126.  
  127.  
  128. ;; Page 30
  129.  
  130. ; Creates and initializes a module variable
  131. (define my-variable 25)
  132. ;Value: my-variable
  133.  
  134. ; Sets the value to 12
  135. (set! my-variable 12)
  136. ;Value: 12
  137.  
  138. ; Returns 30.  Uses lexical variables x and y.
  139. (bind ((x 10) (y 20))
  140.   (+ x y))
  141. ;Value: 30
  142.  
  143. ; Creates an anonymous method, which expects 2 numeric arguments.
  144. (method ((a <number>) (b <number>))
  145.   (list (- a b) (+ a b)))
  146. ;Value: #[method ..]
  147.  
  148. (values 1 2 3)
  149. ;Value[1]: 1
  150. ;Value[2]: 2
  151. ;Value[3]: 3
  152.  
  153. (define-method edges ((center <number>) (radius <number>))
  154.   (values (- center radius) (+ center radius)))
  155. ;Value: edges
  156.  
  157. (edges 100 2)
  158. ;Value[1]: 98
  159. ;Value[2]: 102
  160.  
  161.  
  162. ;; Page 32
  163.  
  164. foo
  165. ;error: unbound variable foo
  166.  
  167. (define foo 10)
  168. ;Value: foo
  169.  
  170. foo
  171. ;Value: 10
  172.  
  173. (+ foo 100)
  174. ;Value: 110
  175.  
  176. bar
  177. ;error: unbound variable bar
  178.  
  179. (define bar foo)
  180. ;Value: bar
  181.  
  182. bar
  183. ;Value: 10
  184.  
  185. (define foo 20)
  186. ;;; Thomas doesn't warn that module variable foo is being redefined.
  187. ;Value: foo
  188.  
  189. foo
  190. ;Value: 20
  191.  
  192. bar
  193. ;Value: 10
  194.  
  195. (+ foo bar)
  196. ;Value: 30
  197.  
  198.  
  199. ;; Page 33
  200.  
  201. (bind ((number1 20)
  202.        (number2 30))
  203.  (+ number1 number2))
  204. ;Value: 50
  205.  
  206. (bind ((x 20)
  207.        (y (+ x x)))
  208.  (+ y y))
  209. ;Value: 80
  210.  
  211. (define foo 10)
  212. ;Value: foo
  213.  
  214. (+ foo foo)
  215. ;Value: 20
  216.  
  217. (bind ((foo 35))
  218.   (+ foo foo))
  219. ;Value: 70
  220.  
  221. (bind ((foo 20))
  222.   (bind ((foo 50))
  223.     (+ foo foo)))
  224. ;Value: 100
  225.  
  226.  
  227. ;; Page 34
  228.  
  229. (bind (((x <integer>) (sqrt 2)))
  230.   x)
  231. ;error: 1.4142135623730951 is not an instance of <integer>
  232.  
  233. (bind ((foo bar baz (values 1 2 3)))
  234.   (list foo bar baz))
  235. ;Value: (1 2 3)
  236.  
  237. (define-method opposite-edges ((center <number>)
  238.                                (radius <number>))
  239.   (bind ((min max (edges center radius)))
  240.     (values max min)))
  241. ;Value: opposite-edges
  242.  
  243. (opposite-edges 100 2)
  244. ;Value[1]: 102
  245. ;Value[2]: 98
  246.  
  247. (bind ((x 10)
  248.        (y 20))
  249.   (bind ((x y (values y x)))
  250.     (list x y)))
  251. ;Value: (20 10)
  252.  
  253. (bind ((!rest nums (edges 100 2)))
  254.   nums)
  255. ;Value: (98 102)
  256.  
  257.  
  258. ;; Page 41
  259.  
  260. (double 10)
  261. ;error: unbound variable double
  262.  
  263. (define-method double ((thing <number>))
  264.   (+ thing thing))
  265. ;Value: double
  266.  
  267. (double 10)
  268. ;Value: 20
  269.  
  270. (double "the rain in Spain.")
  271. ;error: no method for {the generic function double} was found for the
  272. ;       arguments ("the rain in Spain.")
  273.  
  274. (define-method double ((thing <sequence>))
  275.   (concatenate thing thing))
  276. ;Value: double
  277.  
  278. (double "the rain in Spain.")
  279. ;Value: "the rain in Spain.the rain in Spain."
  280.  
  281. (double '(a b c))
  282. ;Value: (a b c a b c)
  283.  
  284.  
  285. ;; Page 43
  286.  
  287. (define-method show-rest (a !rest b)
  288.   (print a)
  289.   (print b)
  290.   #t)
  291. ;Value: show-rest
  292.  
  293. (show-rest 10 20 30 40)
  294. ;10
  295. ;(20 30 40)
  296. ;Value: #t
  297.  
  298. (show-rest 10)
  299. ;10
  300. ;()
  301. ;Value: #t
  302.  
  303.  
  304. ;; Page 44
  305.  
  306. ;;; +
  307. (define-method make-coffee (!rest x) x)
  308. ;Value: make-coffee
  309.  
  310. (define-method percolate (!key (brand 'maxwell-house)
  311.                                (cups 4)
  312.                                (strength 'strong))
  313.   (make-coffee brand cups strength))
  314. ;Value: percolate
  315.  
  316. ;;; +
  317. (define-method position ((x <list>)) (car x))
  318. ;Value: position
  319.  
  320. ;;; +
  321. (add-method position (method ((x <number>)) x))
  322. ;Value[1]: #[method ...]
  323. ;Value[2]: ()
  324.  
  325. ;;; +
  326. (define-method sibling ((x <number>)) (+ x 5))
  327. ;Value: sibling
  328.  
  329. ;;; +
  330. (define-method align-objects (a b c d) (list a b c d))
  331. ;Value: align-objects
  332.  
  333. (define-method layout (widget !key (position: the-pos)
  334.                                    (size: the-size))
  335.   (bind ((the-sibling (sibling widget)))
  336.     (unless (= the-pos (position the-sibling))
  337.       (align-objects widget the-sibling the-pos the-size))))
  338. ;Value: layout
  339.  
  340.  
  341. ;; Page 44
  342.  
  343. (percolate brand: 'folgers cups: 10)
  344. ;Value: (folgers 10 strong)
  345.  
  346. (percolate strength: 'weak
  347.            brand: 'tasters-choice
  348.            cups: 1)
  349. ;Value: (tasters-choice 1 weak)
  350.  
  351. ;;; +
  352. (define my-widget 3)
  353. ;Value: my-widget
  354.  
  355. ;;; +
  356. (define-method point ((x <number>) (y <number>)) (list x y))
  357. ;Value: point
  358.  
  359. ;;; +
  360. (define-method query-user-for-size () 3)
  361. ;Value: query-user-for-size
  362.  
  363. (layout my-widget position: (point 10 10)
  364.                   size: (point 30 50))
  365. ;Value: (3 8 (10 10) (30 50))
  366.  
  367. (layout my-widget size: (query-user-for-size))
  368. ;Value: (3 8 #f 3)
  369.  
  370.  
  371. ;; Page 45
  372.  
  373. ;;; In lieu of format, print lists.
  374. (define-method show-keys (req1 req2 !key foo)
  375.   (print (list "requireds: " req1 req2))
  376.   (print (list "key: " foo))
  377.   #t)
  378. ;Value: show-keys
  379.  
  380. (show-keys 'one 'two foo: 'three)
  381. ;("requireds: " one two)
  382. ;("key: " three)
  383. ;Value: #t
  384.  
  385. (show-keys foo: 'three)
  386. ;("requireds: " foo: three)
  387. ;("key: " #f)
  388. ;Value: #t
  389.  
  390. ;; Page 46
  391.  
  392. (define-method label ((x <object>) !key price)
  393.   (list price x))
  394. ;Value: label
  395.  
  396. ;;; length changed to size
  397. (define-method label ((x <sequence>) !key unit-price)
  398.   (add x (* unit-price (size x))))
  399. ;Value: label
  400.  
  401. (define-method label ((x <list>) !rest info !key calories)
  402.   (add x calories))
  403. ;Value: label
  404.  
  405. (label 'grape price: 189 unit-price: 2)
  406. ;error:  illegal keyword argument unit-price:.  Accepted keyword arguments
  407. ;       are (price:).
  408.  
  409. (label 'grape price: 189)
  410. ;Value: (189 grape)
  411.  
  412. (label (vector 3 4 5) price: 189 unit-price: 2)
  413. ;Value: #(6 3 4 5)
  414.  
  415. (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
  416. ;error:  illegal keyword argument protein:.  Accepted keyword arguments are
  417. ;       (price: unit-price:).
  418.  
  419. (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
  420. ;Value: (9 3 4 5)
  421.  
  422.  
  423. ;; Page 46
  424.  
  425. (define-method test (the-req !rest the-rest !key a b)
  426.   (print the-req)
  427.   (print the-rest)
  428.   (print a)
  429.   (print b))
  430. ;Value: test
  431.  
  432. (test 1 a: 2 b: 3 c: 4)
  433. ;1
  434. ;(a: 2 b: 3 c: 4)
  435. ;2
  436. ;3
  437. ;No value
  438.  
  439.  
  440. ;; Page 49
  441.  
  442. (define-class <point> (<object>)
  443.   horizontal
  444.   vertical)
  445. ;Value: <point>
  446.  
  447.  
  448. ;; Page 49
  449.  
  450. ;;; +
  451. (define my-point (make <point>))
  452. ;Value: my-point
  453.  
  454. (horizontal my-point)
  455. ;;; The example wasn't intended to demonstrate an uninitialized slot, but
  456. ;;; it's a good thing to test here.
  457. ;error: uninitialized slot
  458.  
  459.  
  460. ;; Page 49
  461.  
  462. ;;; +
  463. (define my-point (make <point>))
  464. ;Value: my-point
  465.  
  466. ((setter horizontal) my-point 10)
  467. ;Value: 10
  468.  
  469. ;;; +
  470. (horizontal my-point)
  471. ;Value: 10
  472.  
  473.  
  474. ;; Page 50
  475.  
  476. ;;; +
  477. (define my-point (make <point>))
  478. ;Value: my-point
  479.  
  480. (set! (horizontal my-point) 10)
  481. ;Value: 10
  482.  
  483. ;;; +
  484. (horizontal my-point)
  485. ;Value: 10
  486.  
  487.  
  488. ;; Page 51
  489.  
  490. ;;; Not materially different from the definition of <point> above.
  491.  
  492.  
  493. ;; Page 55
  494.  
  495. (define-class <rectangle> (<object>)
  496.   (top type: <integer>
  497.        init-value: 0
  498.        init-keyword: top:)
  499.   (left type: <integer>
  500.         init-value: 0
  501.         init-keyword: left:)
  502.   (bottom type: <integer>
  503.           init-value: 100
  504.           init-keyword: bottom:)
  505.   (right type: <integer>
  506.          init-value: 100
  507.          init-keyword: right:))
  508. ;Value: <rectangle>
  509.  
  510. <rectangle>
  511. ;Value: #[dylan-class ...]
  512.  
  513. (define my-rectangle (make <rectangle> top: 50 left: 50))
  514. ;Value: my-rectangle
  515.  
  516. (top my-rectangle)
  517. ;Value: 50
  518.  
  519. (bottom my-rectangle)
  520. ;Value: 100
  521.  
  522. (set! (bottom my-rectangle) 55)
  523. ;Value: 55
  524.  
  525. (bottom my-rectangle)
  526. ;Value: 55
  527.  
  528. (set! (bottom my-rectangle) 'foo)
  529. ;error: foo is not an instance of <integer> while executing (setter bottom).
  530.  
  531.  
  532. ;; Page 58
  533.  
  534. ;;; Punt dots.
  535. (define-class <view> (<object>)
  536.   (position allocation: instance))
  537. ;Value: <view>
  538.  
  539. ;;; Punt dots.
  540. (define-class <displaced-view> (<view>)
  541.   (position allocation: virtual))
  542. ;Value: <displaced-view>
  543.  
  544. (define-method position ((v <displaced-view>))
  545.   (displace-transform (next-method v)))
  546. ;Value: position
  547.  
  548. (define-method (setter position) ((v <displaced-view>)
  549.                                   new-position)
  550.   (next-method v (undisplace-transform new-position)))
  551. ;Value: (setter position)
  552.  
  553. ;;; +
  554. (define-method displace-transform (position)
  555.   (list 'displace-transform position))
  556. ;Value: displace-transform
  557.  
  558. ;;; +
  559. (define-method undisplace-transform (position)
  560.   (list 'undisplace-transform position))
  561. ;Value: undisplace-transform
  562.  
  563. ;;; +
  564. (define my-displaced-view (make <displaced-view> position: 'initial-position))
  565. ;Value: my-displaced-view
  566.  
  567. ;;; +
  568. (position my-displaced-view)
  569. ;Value: (displace-transform initial-position)
  570. ;;; Actually getting (displace-transform ()) instead because of limitation
  571. ;;; (9) in DIFFERENCES.
  572.  
  573. ;;; +
  574. (set! (position my-displaced-view) 'next-position)
  575. ;Value: next-position
  576.  
  577. ;;; +
  578. (position my-displaced-view)
  579. ;Value: (displace-transform (undisplace-transform next-position))
  580.  
  581.  
  582. ;; Page 59
  583.  
  584. ;;; Punt dots.
  585. (define-class <shape> (<view>)
  586.   (image allocation: virtual)
  587.   (cached-image allocation: instance init-value: #f))
  588. ;Value: <shape>
  589.  
  590. (define-method image ((shape <shape>))
  591.   (or (cached-image shape)
  592.       (set! (cached-image shape) (compute-image shape))))
  593. ;Value: image
  594.  
  595. (define-method (setter image) ((shape <shape>) new-image)
  596.   (set! (cached-image shape) new-image))
  597. ;Value: (setter image)
  598.  
  599. ;;; +
  600. (define-method compute-image (shape)
  601.   (list 'compute-image shape))
  602. ;Value: compute-image
  603.  
  604. ;;; +
  605. (define my-shape (make <shape>))
  606. ;Value: my-shape
  607.  
  608. ;;; +
  609. (image my-shape)
  610. ;Value: (compute-image #[dylan-instance ...])
  611.  
  612. ;;; +
  613. ((setter image) my-shape 'new-image)
  614. ;Value: new-image
  615.  
  616. ;;; +
  617. (image my-shape)
  618. ;Value: new-image
  619.  
  620.  
  621. ;; Page 61
  622.  
  623. (define foo 10)
  624. ;;; The book shows 10 being returned, but the definition of define says the
  625. ;;; variable name is returned.
  626. ;Value: foo
  627.  
  628. foo                                     ; this is a variable
  629. ;Value: 10                              ; this is the variable's contents
  630.  
  631. (set! foo (+ 10 10))
  632. ;Value: 20
  633.  
  634. foo
  635. ;Value: 20
  636.  
  637. (setter element)                        ; this is a variable
  638. ;Value: #[generic function ...]         ; the variable's contents
  639.  
  640. ;;; +
  641. ;;; Save the original value to restore for later tests.
  642. (define %original-set-element (setter element))
  643. ;Value: %original-set-element
  644.  
  645. ;;; +
  646. (define-method %set-element (seq index value)
  647.   (print (list '%set-element seq index value))
  648.   value)
  649. ;Value: %set-element
  650.  
  651. (set! (setter element) %set-element)
  652. ;Value: #[generic function ...]
  653.  
  654. (id? (setter element) %set-element)
  655. ;Value: #t
  656.  
  657. ;;; The next two should also work.
  658.  
  659. ;;; +
  660. (set! (element 'foo 'bar) 'baz)
  661. ;(%set-element foo bar baz)
  662. ;Value: baz
  663.  
  664. ;;; +
  665. ((setter element) 'foo 'bar 'baz)
  666. ;(%set-element foo bar baz)
  667. ;Value: baz
  668.  
  669. ;;; +
  670. (set! (setter element) %original-set-element)
  671. ;Value: #[generic function ...]
  672.  
  673.  
  674. ;; Page 62
  675.  
  676. (define foo (vector 'a 'b 'c 'd))
  677. ;Value: foo
  678.  
  679. foo
  680. ;Value: #(a b c d)
  681.  
  682. (element foo 2)
  683. ;Value: c
  684.  
  685. (set! (element foo 2) 'sea)
  686. ;Value: sea
  687.  
  688. (element foo 2)
  689. ;Value: sea
  690.  
  691. foo
  692. ;Value: #(a b sea d)
  693.  
  694.  
  695. ;; Page 64
  696.  
  697. ;;; Renamed test to test2, so this definition doesn't conflict with the
  698. ;;; previous definition of test.
  699. (define-method test2 ((thing <object>))
  700.   (if thing
  701.       #t
  702.       #f))
  703. ;Value: test2
  704.  
  705. (test2 'hello)
  706. ;Value: #t
  707.  
  708. (test2 #t)
  709. ;Value: #t
  710.  
  711. (test2 #f)
  712. ;Value: #f
  713.  
  714. (define-method double-negative ((num <number>))
  715.   (if (< num 0)
  716.       (+ num num)
  717.       num))
  718. ;Value: double-negative
  719.  
  720. (double-negative 11)
  721. ;Value: 11
  722.  
  723. (double-negative -11)
  724. ;Value: -22
  725.  
  726.  
  727. ;; Page 65
  728.  
  729. (define-method show-and-tell ((thing <object>))
  730.   (if thing
  731.       (begin
  732.         (print thing)
  733.         #t)
  734.       #f))
  735. ;Value: show-and-tell
  736.  
  737. (show-and-tell "hello")
  738. ;"hello"
  739. ;Value: #t
  740.  
  741.  
  742. ;; Page 65
  743.  
  744. ;;; +
  745. (define-method bonus-illuminated? (pinball post)
  746.   #t)
  747. ;Value: bonus-illuminated?
  748.  
  749. ;;; +
  750. (define-method add-bonus-score (player delta)
  751.   (list 'add-bonus-score player delta))
  752. ;Value: add-bonus-score
  753.  
  754. ;;; +
  755. (define current-player 'current-player)
  756. ;Value: current-player
  757.  
  758. ;;; +
  759. (define pinball 'pinball)
  760. ;Value: pinball
  761.  
  762. ;;; +
  763. (define post 'post)
  764. ;Value: post
  765.  
  766. (when (bonus-illuminated? pinball post)
  767.   (add-bonus-score current-player 100000))
  768. ;Value: (add-bonus-score current-player 100000)
  769.  
  770.  
  771. ;; Page 65
  772.  
  773. ;;; +
  774. (define-method detect-gas? (nose)
  775.   #f)
  776. ;Value: detect-gas?
  777.  
  778. ;;; +
  779. (define-method light (match)
  780.   (print (list 'strike match))
  781.   (print "KABOOM")
  782.   'oh-well)
  783. ;Value: light
  784.  
  785. ;;; +
  786. (define nose 'nose)
  787. ;Value: nose
  788.  
  789. ;;; +
  790. (define match 'match)
  791. ;Value: match
  792.  
  793. (unless (detect-gas? nose)
  794.         (light match))
  795. ;(strike match)
  796. ;"KABOOM"
  797. ;Value: oh-well
  798.  
  799.  
  800. ;; Page 66
  801.  
  802. ;;; +
  803. (define new-position 100)
  804. ;Value: new-position
  805.  
  806. ;;; +
  807. (define old-position 0)
  808. ;Value: old-position
  809.  
  810. (cond ((< new-position old-position)
  811.        "the new position is less")
  812.       ((= new-position old-position)
  813.        "the positions are equal")
  814.       (else: "the new position is greater"))
  815. ;Value: "the new position is greater"
  816.  
  817.  
  818. ;; Page 67
  819.  
  820. ;;; +
  821. (define-method career-choice (student)
  822.   (cond ((id? student 'paul) 'art)
  823.         ((id? student 'jim) 'history)
  824.         ((id? student 'steve) 'science)
  825.         (else: 'bum)))
  826. ;Value: career-choice
  827.  
  828. ;;; Make this a method for easier testing.
  829. (define babble
  830.   (method (student)
  831.     (case (career-choice student)
  832.       ((art music drama)
  833.        (print "Don't quit your day job."))
  834.       ((literature history linguistics)
  835.        (print "That really is fascinating."))
  836.       ((science math engineering)
  837.        (print "Say, can you fix my VCR?"))
  838.       (else: "I wish you luck."))))
  839. ;Value: babble
  840.  
  841. ;;; +
  842. (babble 'neil)
  843. ;Value: "I wish you luck."
  844.  
  845. ;;; +
  846. (babble 'steve)
  847. ;"Say, can you fix my VCR?"
  848. ;No value
  849.  
  850. ;;; +
  851. (babble 'jim)
  852. ;"That really is fascinating."
  853. ;No value
  854.  
  855. ;;; +
  856. (babble 'paul)
  857. ;"Don't quit your day job."
  858. ;No value
  859.  
  860.  
  861. ;; Page 67
  862.  
  863. ;;; Make this a method for easier testing.
  864. (define whatitis
  865.   (method (my-object)
  866.     (select my-object instance?
  867.       ((<window> <view> <rectangle>) "it's a graphic object")
  868.       ((<number> <list> <sequence>) "it's something computational")
  869.       (else: "Don't know what it is"))))
  870. ;Value: whatitis
  871.  
  872. ;;; +
  873. (whatitis (make <view>))
  874. ;Value: "it's a graphic object"
  875.  
  876. ;;; +
  877. (whatitis #())
  878. ;Value: "it's something computational"
  879.  
  880. ;;; +
  881. (whatitis #f)
  882. ;;; MIT-Scheme does not distinguish #f from (), so this actually looks like
  883. ;;; the end of a list -- "it's something computational".
  884. ;Value: "Don't know what it is"
  885.  
  886.  
  887. ;; Page 68
  888.  
  889. (if #t
  890.     (print "it was true")
  891.     #t
  892.     #f)
  893. ;error: too many arguments to if.
  894.  
  895. (if #t
  896.     (begin
  897.       (print "it was true")
  898.       #t)
  899.     #f)
  900. ;"it was true"
  901. ;Value: #t
  902.  
  903.  
  904. ;; Page 69
  905.  
  906. (define-method factorial ((n <integer>))
  907.   (for ((i n (- i 1))                   ;variable clause 1
  908.         (v 1 (* v i)))                  ;variable clause 2
  909.        ((<= i 0) v)))                   ;end test and result
  910. ;Value: factorial
  911.  
  912.  
  913. ;; Page 69
  914.  
  915. (define-method first-even ((s <sequence>))
  916.   (for-each ((number s))
  917.             ((even? number) number)
  918.                                 ; No body forms needed
  919.     ))
  920. ;Value: first-even
  921.  
  922.  
  923. ;; Page 70
  924.  
  925. ;;; +
  926. (define-method schedule-game ((city <symbol>) (year <number>))
  927.   (print (list year city)))
  928. ;Value: schedule-game
  929.  
  930. (define-method schedule-olympic-games ((cities <sequence>)
  931.                                        (start-year <number>))
  932.   (for-each ((year (range from: start-year by: 4))
  933.              (city cities))
  934.             ()                  ; No end test needed.
  935.     (schedule-game city year)))
  936. ;Value: schedule-olympic-games
  937.  
  938. ;;; +
  939. (schedule-olympic-games
  940.  #(boston new-york baltimore chicago denver san-francisco)
  941.  2000)
  942. ;(2000 boston)
  943. ;(2004 new-york)
  944. ;(2008 baltimore)
  945. ;(2012 chicago)
  946. ;(2016 denver)
  947. ;(2020 san-francisco)
  948. ;No value
  949.  
  950.  
  951. ;; Page 70
  952.  
  953. (begin
  954.   (dotimes (i 6) (print "bang!"))
  955.   (print "click!"))
  956. ;"bang!"
  957. ;"bang!"
  958. ;"bang!"
  959. ;"bang!"
  960. ;"bang!"
  961. ;"bang!"
  962. ;"click!"
  963. ;No value
  964.  
  965.  
  966. ;; Page 71
  967.  
  968. (define-method first-even ((seq <sequence>))
  969.   (bind-exit (exit)
  970.     (do (method (item)
  971.           (when (even? item)
  972.             (exit item)))
  973.         seq)))
  974. ;Value: first-even
  975.  
  976. (first-even '(1 3 5 4 7 9 10))
  977. ;Value: 4
  978.  
  979.  
  980. ;; Page 72
  981.  
  982. +
  983. ;Value: #[method ...]
  984.  
  985. '+
  986. ;Value: +
  987.  
  988. (quote +)
  989. ;Value: +
  990.  
  991. ''+
  992. ;Value: (quote +)
  993.  
  994. (+ 10 10)
  995. ;Value: 20
  996.  
  997. '(+ 10 10)
  998. ;Value: (+ 10 10)
  999.  
  1000. (quote (+ 10 10))
  1001. ;Value: (+ 10 10)
  1002.  
  1003.  
  1004. ;; Page 73
  1005.  
  1006. (apply + 1 '(2 3))
  1007. ;Value: 6
  1008.  
  1009. (+ 1 2 3)
  1010. ;Value: 6
  1011.  
  1012. (define math-functions (list + * / -))
  1013. ;Value: math-functions
  1014.  
  1015. math-functions
  1016. ;Value: (#[method ...] #[method ...] #[method ...] #[method ...])
  1017.  
  1018. (first math-functions)
  1019. ;Value: #[method ...]
  1020.  
  1021. (apply (first math-functions) 1 2 '(3 4))
  1022. ;Value: 10
  1023.  
  1024.  
  1025. ;; Page 79
  1026.  
  1027. (method (num1 num2)
  1028.   (+ num1 num2))
  1029. ;Value: #[method ...]
  1030.  
  1031.  
  1032. ;; Page 80
  1033.  
  1034. ;;; +
  1035. (define-class <person> (<object>)
  1036.   (name init-keyword: name:)
  1037.   (age init-keyword: age:))
  1038. ;Value: <person>
  1039.  
  1040. ;;; +
  1041. (define person-list
  1042.   (list (make <person> name: "Paul" age: 15)
  1043.         (make <person> name: "Jill" age: 3)
  1044.         (make <person> name: "Jack" age: 2)
  1045.         (make <person> name: "Peter" age: 12)))
  1046. ;Value: person-list
  1047.  
  1048. ;;; Wrap this in a for-each that shows us the sorted list.
  1049. ;;; Put the test: keyword before the test argument.
  1050. (for-each
  1051.      ((person
  1052.        (sort person-list
  1053.          test: (method (person1 person2)
  1054.                      (< (age person1)
  1055.                         (age person2))))))
  1056.      ()
  1057.   (print (name person)))
  1058. ;"Jack"
  1059. ;"Jill"
  1060. ;"Peter"
  1061. ;"Paul"
  1062. ;Value: #f
  1063.  
  1064. (bind ((double (method (number)
  1065.                  (+ number number))))
  1066.   (double (double 10)))
  1067. ;Value: 40
  1068.  
  1069.  
  1070. ;; Page 80
  1071.  
  1072. (define-method double ((my-method <function>))
  1073.   (method (!rest args)
  1074.     (apply my-method args)
  1075.     (apply my-method args)
  1076.     #f))
  1077. ;Value: double
  1078.  
  1079. ;;; Changed print to display.
  1080. (define print-twice (double display))
  1081. ;Value: print-twice
  1082.  
  1083. print-twice
  1084. ;Value: #[method ...]
  1085.  
  1086. (print-twice "The rain in Spain. . .")
  1087. ;The rain in Spain. . .The rain in Spain. . .
  1088. ;Value: #f
  1089.  
  1090. (print-twice 55)
  1091. ;5555
  1092. ;Value: #f
  1093.  
  1094.  
  1095. ;; Page 81
  1096.  
  1097. ;;; Changed length to size.
  1098. (define-method root-mean-square ((s <sequence>))
  1099.   (bind-methods ((average (numbers)
  1100.                    (/ (reduce1 + numbers)
  1101.                       (size numbers)))
  1102.                  (square (n) (* n n)))
  1103.     (sqrt (average (map square s)))))
  1104. ;Value: root-mean-square
  1105.  
  1106. (root-mean-square '(5 6 6 7 4))
  1107. ;Value: 5.692099788303083
  1108.  
  1109.  
  1110. ;; Page 81
  1111.  
  1112. (define-method newtons-sqrt (x)
  1113.   (bind-methods ((sqrt1 (guess)
  1114.                    (if (close? guess)
  1115.                        guess
  1116.                        (sqrt1 (improve guess))))
  1117.                  (close? (guess)
  1118.                    (< (abs (- (* guess guess) x)) .0001))
  1119.                  (improve (guess)
  1120.                    (/ (+ guess (/ x guess)) 2)))
  1121.     (sqrt1 1)))
  1122. ;Value: newtons-sqrt
  1123.  
  1124. (newtons-sqrt 25)
  1125. ;Value: 5.000000000053723
  1126.  
  1127.  
  1128. ;; Page 82
  1129.  
  1130. (define-method double ((thing <number>))
  1131.   (+ thing thing))
  1132. ;Value: double
  1133.  
  1134.  
  1135. ;; Page 82
  1136.  
  1137. (double 10)
  1138. ;Value: 20
  1139.  
  1140. (double 4.5)
  1141. ;Value: 9.0
  1142.  
  1143.  
  1144. ;; Page 82
  1145.  
  1146. (define-method double ((thing <integer>))
  1147.   (* thing 2))
  1148. ;Value: double
  1149.  
  1150.  
  1151. ;; Page 82
  1152.  
  1153. (define-method double ((thing (singleton 'cup)))
  1154.   'pint)
  1155. ;Value: double
  1156.  
  1157. (double 'cup)
  1158. ;Value: pint
  1159.  
  1160.  
  1161. ;; Page 83
  1162.  
  1163. (define-method double ((num <float>))
  1164.   (print "doubling a floating-point number")
  1165.   (next-method))
  1166. ;Value: double
  1167.  
  1168. (double 10.5)
  1169. ;"doubling a floating-point number"
  1170. ;Value: 21.0
  1171.  
  1172.  
  1173. ;; Page 85
  1174.  
  1175. ;;; +
  1176. (define-class <file> (<object>)
  1177.   name)
  1178. ;Value: <file>
  1179.  
  1180. (define-method show ((device <window>) (thing <character>))
  1181.   (print '(show <window> <character>)))
  1182. ;Value: show
  1183.  
  1184. (define-method show ((device <window>) (thing <string>))
  1185.   (print '(show <window> <string>)))
  1186. ;Value: show
  1187.  
  1188. (define-method show ((device <window>) (thing <rectangle>))
  1189.   (print '(show <window> <rectangle>)))
  1190. ;Value: show
  1191.  
  1192. (define-method show ((device <file>) (thing <character>))
  1193.   (print '(show <file> <character>)))
  1194. ;Value: show
  1195.  
  1196. (define-method show ((device <file>) (thing <string>))
  1197.   (print '(show <file> <string>)))
  1198. ;Value: show
  1199.  
  1200. ;;; +
  1201. (show (make <window>) #\Return)
  1202. ;(show <window> <character>)
  1203. ;No value
  1204.  
  1205. ;;; +
  1206. (show (make <window>) "Return")
  1207. ;(show <window> <string>)
  1208. ;No value
  1209.  
  1210. ;;; +
  1211. (show (make <window>) (make <rectangle>))
  1212. ;(show <window> <rectangle>)
  1213. ;No value
  1214.  
  1215. ;;; +
  1216. (show (make <file>) #\Return)
  1217. ;(show <file> <character>)
  1218. ;No value
  1219.  
  1220. ;;; +
  1221. (show (make <file>) "Return")
  1222. ;(show <file> <string>)
  1223. ;No value
  1224.  
  1225.  
  1226. ;; Page 86
  1227.  
  1228. (make <generic-function> required: 3)
  1229. ;Value: #[generic function ...]
  1230.  
  1231. (make <generic-function> required: 3
  1232.                          debug-name: 'foo)
  1233. ;Value: #[generic function ...]
  1234.  
  1235. (define expand
  1236.   (make <generic-function> required: 1 debug-name: 'expand))
  1237. ;;; The book shows a method being returned, but the definition of define
  1238. ;;; says the variable name is returned.
  1239. ;Value: expand
  1240.  
  1241. (expand 55)
  1242. ;error: no applicable method for 55 in {the generic function expand}
  1243.  
  1244.  
  1245. ;; Page 97
  1246.  
  1247. (define-method double ((thing (singleton 'cup)))
  1248.   'pint)
  1249. ;Value: double
  1250.  
  1251. (double 'cup)
  1252. ;Value: pint
  1253.  
  1254. (double 10)
  1255. ;Value: 20
  1256.  
  1257.  
  1258. ;; Page 98
  1259.  
  1260. (define-method factorial ((num <integer>))
  1261.   (* num (factorial (- num 1))))
  1262. ;Value: factorial
  1263.  
  1264. (define-method factorial ((num (singleton 0)))
  1265.   1)
  1266. ;Value: factorial
  1267.  
  1268. (factorial 5)
  1269. ;Value: 120
  1270.  
  1271.  
  1272. ;; Page 100
  1273.  
  1274. (do (method (a b) (print (+ a b)))
  1275.     '(100 100 200 200)
  1276.     '(1 2 3 4))
  1277. ;101
  1278. ;102
  1279. ;203
  1280. ;204
  1281. ;Value: #f
  1282.  
  1283.  
  1284. ;; Page 101
  1285.  
  1286. (map + '(100 100 200 200)
  1287.        '(1 2 3 4))
  1288. ;Value: (101 102 203 204)
  1289.  
  1290.  
  1291. ;; Page 101
  1292.  
  1293. (map-as <vector> +
  1294.          '(100 100 200 200)
  1295.          '(1 2 3 4))
  1296. ;Value: #(101 102 203 204)
  1297.  
  1298.  
  1299. ;; Page 101
  1300.  
  1301. (define x '(100 100 200 200))
  1302. ;Value: x
  1303.  
  1304. (map-into x + '(1 2 3 4))
  1305. ;Value: (101 102 203 204)
  1306.  
  1307. x
  1308. ;Value: (101 102 203 204)
  1309.  
  1310.  
  1311. ;; Page 102
  1312.  
  1313. (any? > '(1 2 3 4) '(5 4 3 2))
  1314. ;Value: #t
  1315.  
  1316. (any? even? '(1 3 5 7))
  1317. ;Value: #f
  1318.  
  1319.  
  1320. ;; Page 102
  1321.  
  1322. (every? > '(1 2 3 4) '(5 4 3 2))
  1323. ;Value: #f
  1324.  
  1325. (every? odd? '(1 3 5 7))
  1326. ;Value: #t
  1327.  
  1328.  
  1329. ;; Page 102
  1330.  
  1331. (define high-score 10)
  1332. ;Value: high-score
  1333.  
  1334. (reduce max high-score '(3 1 4 1 5 9))
  1335. ;Value: 10
  1336.  
  1337. (reduce max high-score '(3 12 9 8 8 6))
  1338. ;Value: 12
  1339.  
  1340.  
  1341. ;; Page 103
  1342.  
  1343. (reduce1 + '(1 2 3 4 5))
  1344. ;Value: 15
  1345.  
  1346.  
  1347. ;; Page 103
  1348.  
  1349. (define flavors #(chocolate pistachio pumpkin))
  1350. ;Value: flavors
  1351.  
  1352. (member? 'chocolate flavors)
  1353. ;Value: #t
  1354.  
  1355. (member? 'banana flavors)
  1356. ;Value: #f
  1357.  
  1358.  
  1359. ;; Page 103
  1360.  
  1361. ;;; +
  1362. (define-method has-nuts? (flavor)
  1363.   (member? flavor #(pistachio butter-pecan macadamia)))
  1364. ;Value: has-nuts?
  1365.  
  1366. flavors
  1367. ;Value: #(chocolate pistachio pumpkin)
  1368.  
  1369. (find-key flavors has-nuts?)
  1370. ;Value: 1
  1371.  
  1372. (element flavors 1)
  1373. ;Value: pistachio
  1374.  
  1375.  
  1376. ;; Page 104
  1377.  
  1378. (define numbers (list 10 13 16 19))
  1379. ;Value: numbers
  1380.  
  1381. (replace-elements! numbers odd? double)
  1382. ;Value: (10 26 16 38)
  1383.  
  1384.  
  1385. ;; Page 104
  1386.  
  1387. (define x (list 'a 'b 'c 'd 'e 'f))
  1388. ;Value: x
  1389.  
  1390. (fill! x 3 start: 2)
  1391. ;Value: (a b 3 3 3 3)
  1392.  
  1393.  
  1394. ;; Page 105
  1395.  
  1396. (define numbers '(3 4 5))
  1397. ;Value: numbers
  1398.  
  1399. (add numbers 1)
  1400. ;Value: (1 3 4 5)
  1401.  
  1402. numbers
  1403. ;Value: (3 4 5)
  1404.  
  1405.  
  1406. ;; Page 105
  1407.  
  1408. (define numbers (list 3 4 5))
  1409. ;Value: numbers
  1410.  
  1411. (add! numbers 1)
  1412. ;Value: (1 3 4 5)
  1413.  
  1414.  
  1415. ;; Page 105
  1416.  
  1417. (add-new '(3 4 5) 1)
  1418. ;Value: (1 3 4 5)
  1419.  
  1420. (add-new '(3 4 5) 4)
  1421. ;Value: (3 4 5)
  1422.  
  1423.  
  1424. ;; Page 105
  1425.  
  1426. (add-new! (list 3 4 5) 1)
  1427. ;Value: (1 3 4 5)
  1428.  
  1429. (add-new! (list 3 4 5) 4)
  1430. ;Value: (3 4 5)
  1431.  
  1432.  
  1433. ;; Page 106
  1434.  
  1435. (remove '(3 1 4 1 5 9) 1)
  1436. ;Value: (3 4 5 9)
  1437.  
  1438.  
  1439. ;; Page 106
  1440.  
  1441. (remove! (list 3 1 4 1 5 9) 1)
  1442. ;Value: (3 4 5 9)
  1443.  
  1444.  
  1445. ;; Page 106
  1446.  
  1447. (choose even? '(3 1 4 1 5 9))
  1448. ;Value: (4)
  1449.  
  1450.  
  1451. ;; Page 106
  1452.  
  1453. (choose-by even? (range from: 1)
  1454.                  '(a b c d e f g h i))
  1455. ;Value: (b d f h)
  1456.  
  1457.  
  1458. ;; Page 107
  1459.  
  1460. (intersection '(john paul george ringo)
  1461.               '(richard george edward charles john))
  1462. ;Value: (john george)
  1463.  
  1464.  
  1465. ;; Page 107
  1466.  
  1467. (union '(butter flour sugar salt eggs)
  1468.        '(eggs butter mushrooms onions salt))
  1469. ;;; The union may have the elements in a different order.
  1470. ;Value: (salt butter flour sugar eggs mushrooms onions)
  1471.  
  1472.  
  1473. ;; Page 107
  1474.  
  1475. (remove-duplicates '(spam eggs spam sausage spam spam spam))
  1476. ;Value: (spam eggs sausage)
  1477.  
  1478.  
  1479. ;; Page 108
  1480.  
  1481. (remove-duplicates! '(spam eggs spam sausage spam spam))
  1482. ;Value: (spam eggs sausage)
  1483.  
  1484.  
  1485. ;; Page 108
  1486.  
  1487. (define hamlet '(to be or not to be))
  1488. ;Value: hamlet
  1489.  
  1490. (id? hamlet (copy-sequence hamlet))
  1491. ;Value: #f
  1492.  
  1493. (copy-sequence hamlet start: 2 end: 4)
  1494. ;Value: (or not)
  1495.  
  1496.  
  1497. ;; Page 108
  1498.  
  1499. (concatenate-as <string> '(#\n #\o #\n) '(#\f #\a #\t))
  1500. ;Value: "nonfat"
  1501.  
  1502. (concatenate-as <vector> '(0 1 2) '(3 4 5) '(6 7 8))
  1503. ;Value: #(0 1 2 3 4 5 6 7 8)
  1504.  
  1505.  
  1506. ;; Page 108
  1507.  
  1508. (concatenate "low-" "calorie")
  1509. ;Value: "low-calorie"
  1510.  
  1511. (concatenate '(0 1 2) '(3 4 5) '(6 7 8))
  1512. ;Value: (0 1 2 3 4 5 6 7 8)
  1513.  
  1514.  
  1515. ;; Page 109
  1516.  
  1517. (define phrase "I hate oatmeal.")
  1518. ;Value: phrase
  1519.  
  1520. (replace-subsequence! phrase "like" start: 2)
  1521. ;Value: "I like oatmeal."
  1522.  
  1523.  
  1524. ;; Page 109
  1525.  
  1526. (define x '(bim bam boom))
  1527. ;Value: x
  1528.  
  1529. (reverse x)
  1530. ;Value: (boom bam bim)
  1531.  
  1532. x
  1533. ;Value: (bim bam boom)
  1534.  
  1535.  
  1536. ;; Page 109
  1537.  
  1538. (reverse! '(bim bam boom))
  1539. ;Value: (boom bam bim)
  1540.  
  1541.  
  1542. ;; Page 110
  1543.  
  1544. (define numbers '(3 1 4 1 5 9))
  1545. ;Value: numbers
  1546.  
  1547. (sort numbers)
  1548. ;Value: (1 1 3 4 5 9)
  1549.  
  1550. numbers
  1551. ;Value: (3 1 4 1 5 9)
  1552.  
  1553.  
  1554. ;; Page 110
  1555.  
  1556. (sort! '(3 1 4 1 5 9))
  1557. ;Value: (1 1 3 4 5 9)
  1558.  
  1559.  
  1560. ;; Page 110
  1561.  
  1562. (last '(emperor of china))
  1563. ;Value: china
  1564.  
  1565.  
  1566. ;; Page 111
  1567.  
  1568. (subsequence-position "Ralph Waldo Emerson" "Waldo")
  1569. ;Value: 6
  1570.  
  1571.  
  1572. ;; Page 113
  1573.  
  1574. (aref #(7 8 9) 1)
  1575. ;Value: 8
  1576.  
  1577.  
  1578. ;; Page 113
  1579.  
  1580. ;;; +
  1581. (define x #(7 8 9))
  1582. ;Value: x
  1583.  
  1584. ;;; Using "x" rather than "#(7 8 9)"
  1585. (set! (aref x 1) 5)
  1586. ;buggy example.  Should return 5
  1587. ;Value: 5
  1588.  
  1589. ;;; +
  1590. x
  1591. ;Value: #(7 5 9)
  1592.  
  1593. ;;; +
  1594. (define x #(7 8 9))
  1595. ;Value: x
  1596.  
  1597. ;;; Using "x" rather than "#(7 8 9)"
  1598. ((setter aref) x 1 5)
  1599. ;buggy example.  Should return 5
  1600. ;Value: 5
  1601.  
  1602. ;;; +
  1603. x
  1604. ;Value: #(7 5 9)
  1605.  
  1606.  
  1607. ;; Page 113
  1608.  
  1609. (dimensions (make <array> dimensions: '(4 4)))
  1610. ;Value: (4 4)
  1611.  
  1612.  
  1613. ;; Page 115
  1614.  
  1615. (cons 1 2)
  1616. ;Value: (1 . 2)
  1617.  
  1618. (cons 1 '(2 3 4 5))
  1619. ;Value: (1 2 3 4 5)
  1620.  
  1621.  
  1622. ;; Page 115
  1623.  
  1624. (list 1 2 3)
  1625. ;Value: (1 2 3)
  1626.  
  1627. (list (+ 4 3) (- 4 3))
  1628. ;Value: (7 1)
  1629.  
  1630.  
  1631. ;; Page 115
  1632.  
  1633. (list* 1 2 3 '(4 5 6))
  1634. ;Value: (1 2 3 4 5 6)
  1635.  
  1636.  
  1637. ;; Page 116
  1638.  
  1639. (car '(4 5 6))
  1640. ;Value: 4
  1641.  
  1642. (car '())
  1643. ;Value: ()
  1644.  
  1645.  
  1646. ;; Page 116
  1647.  
  1648. (cdr '(4 5 6))
  1649. ;Value: (5 6)
  1650.  
  1651. (cdr '())
  1652. ;Value: ()
  1653.  
  1654.  
  1655. ;; Page 116
  1656.  
  1657. (define x '(4 5 6))
  1658. ;;; The book shows (4 5 6) being returned, but the definition of define
  1659. ;;; says the variable name is returned.
  1660. ;Value: x
  1661.  
  1662. (set! (car x) 9)
  1663. ;Value: 9
  1664.  
  1665. ;;; +
  1666. x
  1667. ;Value: (9 5 6)
  1668.  
  1669.  
  1670. ;; Page 116
  1671.  
  1672. (define x '(4 5 6))
  1673. ;;; The book shows (4 5 6) being returned, but the definition of define
  1674. ;;; says the variable name is returned.
  1675. ;Value: x
  1676.  
  1677. (set! (cdr x) '(a b c))
  1678. ;Value: (a b c)
  1679.  
  1680. ;;; +
  1681. x
  1682. ;Value: (4 a b c)
  1683.  
  1684.  
  1685. ;; Page 120
  1686.  
  1687. (define x "Van Gogh")
  1688. ;Value: x
  1689.  
  1690. (as-lowercase x)
  1691. ;Value: "van gogh"
  1692.  
  1693. ;;; +
  1694. x
  1695. ;Value: "Van Gogh"
  1696.  
  1697.  
  1698. ;; Page 120
  1699.  
  1700. (define x "Van Gogh")
  1701. ;Value: x
  1702.  
  1703. (as-lowercase! x)
  1704. ;Value: "van gogh"
  1705.  
  1706. ;;; +
  1707. x
  1708. ;Value: "van gogh"
  1709.  
  1710.  
  1711. ;; Page 120
  1712.  
  1713. (define x "Van Gogh")
  1714. ;Value: x
  1715.  
  1716. (as-uppercase x)
  1717. ;Value: "VAN GOGH"
  1718.  
  1719. ;;; +
  1720. x
  1721. ;Value: "Van Gogh"
  1722.  
  1723.  
  1724. ;; Page 120
  1725.  
  1726. (define x "Van Gogh")
  1727. ;Value: x
  1728.  
  1729. ;;; as-uppercase changed to as-uppercase!
  1730. (as-uppercase! x)
  1731. ;Value: "VAN GOGH"
  1732.  
  1733. ;;; +
  1734. x
  1735. ;Value: "VAN GOGH"
  1736.  
  1737.  
  1738. ;; Page 123
  1739.  
  1740. ;;; NOT tested.  From here through page 130, the "examples" are
  1741. ;;; explanations, not true examples.
  1742.  
  1743.  
  1744. ;; Page 142
  1745.  
  1746. ;;; Make this a method for easier testing.
  1747. (define handleit
  1748.   (method (it)
  1749.     (handler-case (it)
  1750.       ((<type-error>) "there was a type-error")
  1751.       ((<error>) "there was an error")
  1752.       ((<warning>) "there was a warning"))))
  1753. ;Value: handleit
  1754.  
  1755. ;;; +
  1756. (handleit (method ()
  1757.              (signal (make <simple-warning>
  1758.                            format-string: "simple warning"
  1759.                            format-arguments: '()))))
  1760. ;Value: "there was a warning"
  1761.  
  1762. ;;; +
  1763. (handleit (method ()
  1764.              (check-type 'foo <string>)))
  1765. ;Value: "there was a type-error"
  1766.  
  1767. ;;; +
  1768. (handleit (method ()
  1769.             (error "simple error")))
  1770. ;Value: "there was an error"
  1771.  
  1772.  
  1773. ;; Page 144-146
  1774.  
  1775. ;;; This example demonstrates handling a <file-not-found> error by
  1776. ;;; signaling a <try-a-different-file> restart.  This example requires
  1777. ;;; implementation-specific code, so it might be found in a file named
  1778. ;;; "restart-example.dyl" in the implementation-specific subdirectories.
  1779.  
  1780.  
  1781. ;; Page 153
  1782.  
  1783. (as <symbol> "foo")
  1784. ;Value: foo
  1785.  
  1786. (id? 'FOO (as <symbol> "Foo"))
  1787. ;Value: #t
  1788.  
  1789. 'Foo
  1790. ;Value: foo
  1791.  
  1792. (as <keyword> "foo")
  1793. ;Value: foo:
  1794.  
  1795.  
  1796. ;; Page 154
  1797.  
  1798. (as <string> 'Foo)
  1799. ;Value: "foo"
  1800.  
  1801. (as <string> 'bar:)
  1802. ;Value: "bar"
  1803.  
  1804.  
  1805. ;; Page 157
  1806.  
  1807. (define-method sum ((numbers <sequence>))
  1808.   (reduce1 + numbers))
  1809. ;Value: sum
  1810.  
  1811. (define-method square ((x <number>)) (* x x))
  1812. ;Value: square
  1813.  
  1814. (define-method square-all ((coords <sequence>))
  1815.   (map square coords))
  1816. ;Value: square-all
  1817.  
  1818. (define distance (compose sqrt sum square-all))
  1819. ;Value: distance
  1820.  
  1821. (distance '(3 4 5))
  1822. ;Value: 7.0710678118654755
  1823.  
  1824.  
  1825. ;; Page 157
  1826.  
  1827. ;;; +
  1828. (define-method female? (name)
  1829.   (member? name #(michelle anne ann barbara roseanne susan)))
  1830. ;Value: female?
  1831.  
  1832. (map female? '(michelle arnold roseanne))
  1833. ;Value: (#t #f #t)
  1834.  
  1835. (map (complement female?) '(michelle arnold roseanne))
  1836. ;Value: (#f #t #f)
  1837.  
  1838.  
  1839. ;;Page 158
  1840.  
  1841. (map (curry + 1) '(3 4 5))
  1842. ;Value: (4 5 6)
  1843.  
  1844.  
  1845. ;; Page 158
  1846.  
  1847. (define yuppify (rcurry concatenate ", ayup"))
  1848. ;Value: yuppify
  1849.  
  1850. (yuppify "I'm from New Hampsha")
  1851. ;Value: "I'm from New Hampsha, ayup"
  1852.  
  1853.  
  1854. ;; Page 159
  1855.  
  1856. ((always 1) 'x 'y 'z)
  1857. ;Value: 1
  1858.  
  1859. ((always #t) #f #f)
  1860. ;Value: #t
  1861.